<?php
namespace app\ebcms\controller;
class Datadict extends \app\ebcms\controller\Common
{
    
    public function index()
    {
        $tpl = '';
        if (input('tpl')) {
            $tpl = 'cate';
        }elseif (input('?id')) {
            $tpl = 'datadict';
        }
        return $this->fetch($tpl);
    }

    public function add()
    {
        if (request()->isGet()) {
            $cate = \think\Db::name('datadictcate')->find(input('category_id'));
            return \ebcms\Form::fetch([], ['ext_id' => $cate['extend_id']]);
        } elseif (request()->isPost()) {
            $obj = new \app\ebcms\model\Datadict();
            if (false !== $obj ->validate('datadict.add')->save(input())) {
                $this -> success('操作成功！');
            }else{
                $this -> error($obj -> getError());
            }
        }
    }

    public function edit($id)
    {
        if (request()->isGet()) {
            $data = \app\ebcms\model\Datadict::with('datadictcate')->find(input('id'));
            return \ebcms\Form::fetch($data, ['ext_id' => $data['datadictcate']['extend_id']]);
        } elseif (request()->isPost()) {
            $obj = \app\ebcms\model\Datadict::get(input('id'));
            if (false !== $obj -> validate('datadict.edit') -> isUpdate(true) -> save(input())) {
                $this -> success('操作成功！');
            }else{
                $this -> error($obj -> getError());
            }
        }
    }

    public function status(){
        \app\ebcms\model\Datadict::where(['id'=>['in',input('ids')]]) -> setField('status',input('value')?1:0);
        $this -> success('操作成功！');
    }

    public function resort(){
        \app\ebcms\model\Datadict::where('id',input('id')) -> setField('sort',input('value'));
        $this -> success('操作成功！');
    }

    public function delete()
    {
        \app\ebcms\model\Datadict::where(['id'=>['in',input('ids')]]) -> delete();
        $this -> success('删除成功！');
    }

}